package com.bingxue.edu.management.shop.service;

import com.bingxue.edu.framework.common.util.ValidationUtils;
import com.bingxue.edu.framework.mybatis.base.BaseService;
import com.bingxue.edu.management.education.model.entity.LessonStudent;
import com.bingxue.edu.management.education.model.req.ClassCancelReq;
import com.bingxue.edu.management.shop.mapper.OrderItemMapper;
import com.bingxue.edu.management.shop.mapper.OrderMapper;
import com.bingxue.edu.management.shop.mapper.RefundMapper;
import com.bingxue.edu.management.shop.model.entity.Order;
import com.bingxue.edu.management.shop.model.entity.OrderItem;
import com.bingxue.edu.management.shop.model.entity.Refund;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

import static com.bingxue.edu.common.util.EduUtil.generateRefundId;

@Slf4j
@Service
@RequiredArgsConstructor
public class RefundService extends BaseService<RefundMapper, Refund> {

    private final OrderMapper orderMapper;
    private final OrderItemMapper orderItemMapper;

    @Transactional(rollbackFor = Exception.class)
    public void updateRefund(Refund refund) {
        if (refund.getRefundOff()) {
            Order order = orderMapper.selectOneById(refund.getOrderId());
            ValidationUtils.throwIfNull(order, "order为空");
            BigDecimal refundPrice1 = order.getRefundPrice();
            if (refundPrice1 == null) {
                refundPrice1 = BigDecimal.ZERO;
            }
            refundPrice1 = refundPrice1.add(refund.getRefundPrice());
            order.setRefundPrice(refundPrice1);
            orderMapper.update(order);

            OrderItem orderItem = orderItemMapper.selectOneById(refund.getOrderItemId());
            ValidationUtils.throwIfNull(orderItem, "orderItem为空");
            BigDecimal refundPrice2 = orderItem.getRefundPrice();
            if (refundPrice2 == null) {
                refundPrice2 = BigDecimal.ZERO;
            }
            refundPrice2 = refundPrice2.add(refund.getRefundPrice());
            orderItem.setRefundPrice(refundPrice2);
            orderItemMapper.update(orderItem);
        }

        this.updateById(refund);
    }

    public Refund initNewRefund(ClassCancelReq req, OrderItem orderItem, String enrollmentId, List<LessonStudent> cancelLessonStudentList) {
        // 计算退款金额
        BigDecimal refundPrice = cancelLessonStudentList.stream().map(LessonStudent::getLessonPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        // 退课顺序列表
        List<Integer> cancelLessonIndexes = cancelLessonStudentList.stream().map(LessonStudent::getLessonIndex).sorted().toList();

        Refund refund = new Refund();
        refund.setId(generateRefundId());
        refund.setOrderId(orderItem.getOrderId());
        refund.setOrderItemId(orderItem.getId());
        refund.setEnrollmentId(enrollmentId);
        refund.setStudentId(req.getStudentId());
        refund.setClassId(req.getClassId());
        refund.setCancelLessonIndexes(cancelLessonIndexes);
        refund.setRefundPrice(refundPrice);
        refund.setRefundOff(false);
        refund.setChangeType(req.getChangeType());
        refund.setOperateSource(req.getOperateSource());

        return refund;
    }
}
